package com.shuorikeji.huohuo.ui.me;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.UserInfoBean;
import com.shuorikeji.huohuo.bean.UserInfoMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MyQbActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_ye)
    TextView tvYe;
    @BindView(R.id.tv_tx)
    TextView tvTx;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlView;
    @BindView(R.id.rv_data)
    RecyclerView rvData;

    private UserInfoBean userInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_my_qb);
    }

    @Override
    protected void initData() {
        initSmartRefreshLayout();
        getUserInfoData();
        getQbDsyMx();
        initClick();
    }

    private void initSmartRefreshLayout() {
        srlView.setRefreshHeader(new ClassicsHeader(MyQbActivity.this));
        srlView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                srlView.finishRefresh();
                srlView.finishLoadMore();
            }
        });
        srlView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                srlView.finishRefresh();
                srlView.finishLoadMore();
            }
        });
    }

    private void getUserInfoData() {
        mPresenter.getUserInfo(false, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        UserInfoMainBean data = (UserInfoMainBean) object;
                        userInfoBean = data.getData();
                        try {
                            double douYe = Double.parseDouble(userInfoBean.getMoney());
                            String newYe = String.format("%.2f", douYe);
                            tvYe.setText(newYe);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MyQbActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void getQbDsyMx() {
        mPresenter.qbDsyMx(true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MyQbActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        tvTx.setOnClickListener(view -> {
            Intent intent = new Intent(MyQbActivity.this, TxActivity.class);
            intent.putExtra("YE", userInfoBean.getMoney());
            startActivity(intent);
        });
    }
}